package com.huilian.cloud.module.system.service.frontendpermission;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.huilian.cloud.module.system.controller.admin.frontendpermission.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.frontendpermission.FrontendPermissionDO;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.util.object.BeanUtils;

import com.huilian.cloud.module.system.dal.mysql.frontendpermission.FrontendPermissionMapper;

import static com.huilian.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.huilian.cloud.module.system.enums.ErrorCodeConstants.*;

/**
 * 前端权限 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FrontendPermissionServiceImpl implements FrontendPermissionService {

    @Resource
    private FrontendPermissionMapper frontendPermissionMapper;

    @Override
    public String createFrontendPermission(FrontendPermissionSaveReqVO createReqVO) {
        // 插入
        FrontendPermissionDO frontendPermission = BeanUtils.toBean(createReqVO, FrontendPermissionDO.class);
        frontendPermissionMapper.insert(frontendPermission);
        // 返回
        return frontendPermission.getFrontendPermission();
    }

    @Override
    public void updateFrontendPermission(FrontendPermissionSaveReqVO updateReqVO) {
        // 校验存在
        //validateFrontendPermissionExists(updateReqVO.getId());
        // 更新
        FrontendPermissionDO updateObj = BeanUtils.toBean(updateReqVO, FrontendPermissionDO.class);
        frontendPermissionMapper.updateById(updateObj);
    }



    private void validateFrontendPermissionExists(String id) {
        if (frontendPermissionMapper.selectById(id) == null) {
            throw exception(FRONTEND_PERMISSION_NOT_EXISTS);
        }
    }

    @Override
    public FrontendPermissionDO getFrontendPermission(String id) {
        return frontendPermissionMapper.selectById(id);
    }

    @Override
    public PageResult<FrontendPermissionDO> getFrontendPermissionPage(FrontendPermissionPageReqVO pageReqVO) {
        return frontendPermissionMapper.selectPage(pageReqVO);
    }

}